package com.jianjun.project.system.dict;

import cn.hutool.core.collection.CollectionUtil;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jianjun.common.util.DictUtils;
import com.jianjun.project.common.model.SysDictData;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author source
 * {@code @date} 2025/1/15 15:39
 */
public class SysDictDataService {
    private final SysDictData dao = new SysDictData().dao();

    public Page<SysDictData> paginate(SysDictData model) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysDictData", Kv.by("cond", model.toMap()).set(model.getColumnAsc()));
        return dao.paginate(model.getPageNum(), model.getPageSize(), sqlParaDb);
    }

    /**
     * 根据条件分页查询字典类型
     */
    public List<SysDictData> selectDictDataList(Model<SysDictData> model) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysDictData", Kv.by("cond", model.toMap()));
        return dao.find(sqlParaDb);
    }

    public int countDictDataByType(String dictType) {
        SysDictData model = new SysDictData();
        model.setDictType(dictType);
        SqlPara sqlParaDb = Db.getSqlPara("querySysDictData", Kv.by("cond", model.toMap()));
        return Db.queryInt(sqlParaDb.getSql().replace("*", "count(1)"), sqlParaDb.getPara());
    }

    public SysDictData selectDictDataById(Long dictCode) {
        return dao.findById(dictCode);
    }


    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    public boolean insertDictData(SysDictData data) {
        data.setCreateTime(new Date());
        boolean res = data.save();
        if (res) {
            List<SysDictData> dictDatas = selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return res;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    public boolean updateDictData(SysDictData data) {
        data.setUpdateTime(new Date());
        boolean res = data.update();
        if (res) {
            List<SysDictData> dictDatas = selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return res;
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (CollectionUtil.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        SysDictData model = new SysDictData();
        model.setStatus("0");
        model.setDictType(dictType);
        dictDatas = selectDictDataList(model).stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(dictDatas)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            dao.deleteById(dictCode);
            List<SysDictData> dictDatas = selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }
}
